#ifndef MEDIACLASS
#define MEDIACLASS

#include "graph.h"
#include <iostream>
#include "QObject"
#include <QString>
#include "Qt"

class MediaClass : public QObject{
    Q_OBJECT
    Q_PROPERTY(QList<QList<int> > matrix READ matrix WRITE setMatrix)
    Q_PROPERTY(QString adj READ adj)
    Q_PROPERTY(QString bfs READ bfs)
    Q_PROPERTY(QString dfs READ dfs)
public:
    Graph * graph;
    MediaClass(){
        graph = new Graph();
        graph->vertexCount = 9;
    }

    QString adj(){
        QString * temp= new QString;
        graph->buildAdj();
        *temp = QString::fromStdString(graph->printAdj());
        return *temp;
    }

    QString bfs(){
        return bfs_str;
    }

    QString dfs(){
        return dfs_str;
    }

    QList<QList<int> > matrix(){
        int row = graph->vertexCount;
        QList<QList<int> > * a = new QList<QList<int> > ;
        for(int i = 0; i < row; i++)
        {
            for(int j = 0; j < row; j++){
                (*a)[i][j] = graph->Matrix[i][j];
            }
        }
        return *a;
    }

    Q_INVOKABLE void setMatrix(QList<QList<int> > newMatrix){
        int row = newMatrix.length();
        for(int i = 0; i < row; i++){
            for(int j = 0; j < row; j++){
                graph->Matrix[i][j] = 0;
            }
        }
        for(int i = 0; i < row; i++){
            for(int j = 0; j < row; j++){
                graph->Matrix[i][j] = newMatrix[i][j];
            }
        }
    }

    Q_INVOKABLE int getValue(int i, int j){
        return graph->Matrix[i][j];
    }

    Q_INVOKABLE void setValue(int i, int j, int s){
        graph->Matrix[i][j] = s;
    }

    Q_INVOKABLE void buildbfs(int i){
        bfs_str = QString("DFS:\n");
        bfs_str.append(QString::fromStdString(graph->BFS(i)));
    }

    Q_INVOKABLE void builddfs(int i){
//        dfs_str = QString::非递归方式fromStdString(graph->DFS(i));
        dfs_str = QString("BFS:\n递归方式\n");
        dfs_str.append(QString::fromStdString(graph->DFS_recur(i)));
        dfs_str.append("\n非递归方式\n");
        dfs_str.append(QString::fromStdString(graph->DFS(i)));
    }

private:
    QString bfs_str;
    QString dfs_str;
};

#endif // MEDIACLASS

